package cn.cleanarch.infra.idp.s3.service.impl;

import cn.cleanarch.infra.idp.s3.dao.repository.S3ConfigRepository;
import cn.cleanarch.infra.idp.s3.domain.entity.S3Config;
import cn.cleanarch.infra.idp.s3.service.S3ConfigCacheService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * S3配置缓存服务实现类
 */
@Service
public class S3ConfigCacheServiceImpl implements S3ConfigCacheService {

    private final S3ConfigRepository s3ConfigRepository;

    public S3ConfigCacheServiceImpl(S3ConfigRepository s3ConfigRepository) {
        this.s3ConfigRepository = s3ConfigRepository;
    }

    @Override
    public S3Config getCachedById(Long id) {
        return s3ConfigRepository.getById(id);
    }

    @Override
    public S3Config getCachedByBusinessType(String businessType) {
        QueryWrapper<S3Config> wrapper = new QueryWrapper<>();
        wrapper.eq("business_type", businessType);
        return s3ConfigRepository.getOne(wrapper, false);
    }

    @Override
    public List<S3Config> getCachedAllEnabledConfigs() {
        QueryWrapper<S3Config> wrapper = new QueryWrapper<>();
        wrapper.eq("enabled", true);
        wrapper.orderByDesc("create_time");
        return s3ConfigRepository.list(wrapper);
    }

    @Override
    public void evictConfigCache(Long id) {
        // 缓存注解会自动处理清除操作
    }

    @Override
    public void evictAllConfigCache() {
        // 缓存注解会自动处理清除操作
    }
}